package com.turnengine.client.local.faction.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.local.faction.bean.FactionSerializer;
import com.turnengine.client.local.faction.bean.IFaction;
import com.turnengine.client.local.faction.enums.FactionUnited;
import java.io.IOException;

/**
 * The Add Faction Data Serializer.
 */
public class AddFactionDataSerializer extends AbstractCommandDataSerializer<IFaction> implements IAddFactionDataSerializer {

	/**
	 * Creates a new Add Faction Data Serializer.
	 */
	public AddFactionDataSerializer() {
		super(AddFaction.COMMAND_ID);
	}

	@Override
	public IFaction readReturnValue(IDataReader reader) throws IOException {
		IFaction value = new FactionSerializer().readObject(reader);
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, IFaction value) throws IOException {
		new FactionSerializer().writeObject(writer, value);
	}

	@Override
	public IExecutableCommand<IFaction> readRequest(IDataReader reader) throws IOException {

		// Read fields
		long loginId = reader.readLong();
		int instanceId = reader.readInt();
		String name = reader.readString(true);
		String password = reader.readString(true);
		FactionUnited united = reader.readEnum(FactionUnited.class, true);
		int playerLimit = reader.readInt();

		// Create object
		AddFaction object = new AddFaction();
		object.setLoginId(loginId);
		object.setInstanceId(instanceId);
		object.setName(name);
		object.setPassword(password);
		object.setUnited(united);
		object.setPlayerLimit(playerLimit);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<IFaction> argument) throws IOException {
		AddFaction object = (AddFaction) argument;

		// Get fields
		long loginId = object.getLoginId();
		int instanceId = object.getInstanceId();
		String name = object.getName();
		String password = object.getPassword();
		FactionUnited united = object.getUnited();
		int playerLimit = object.getPlayerLimit();

		// Write fields
		writer.writeLong(loginId);
		writer.writeInt(instanceId);
		writer.writeString(name, true);
		writer.writeString(password, true);
		writer.writeEnum(united, true);
		writer.writeInt(playerLimit);
	}

	@Override
	public IExecutableCommandResponse<IFaction> newResponse() {
		return new AddFactionResponse();
	}
}
